package com.kaifamiao.map;

import java.util.Map;
import java.util.Collection;
import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Objects;

// 用自定义的 Pairs类 实现 Map接口
public class Pairs<K,V> implements Map<K,V> {

    // 用来存放 键值对 的数组
    private Node<K,V>[] table;
    // 用来记录 键值对 个数的实例变量
    private int count;

    public Pairs(){
        table = new Node[16];
    }

    // 用 嵌套类 实现 Map接口内部的 Entry接口
    private static class Node<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;

        public Node(K key, V value){
            this.key = key;
            this.value = value;
        }

        @Override
        public K getKey() {
            return key;
        }

        @Override
        public V getValue() {
            return value;
        }

        @Override
        public V setValue(V value) {
            V old = this.value; // 缓存老值
            this.value = value; // 用新值覆盖老值
            return old;
        }
    }

    @Override
    public V put(K key, V value) {

        int index = indexOf( key );
        if( index != -1 ) {
            Node<K,V> node = table[ index ] ;
            return node.setValue( value );
        }

        Node<K,V> newNode = new Node<>(key,value);
        table[ count++ ] = newNode;
        return null;
    }

    @Override
    public V get(Object key) {
        return null;
    }

    @Override
    public V remove(Object key) {
        return null;
    }

    private int indexOf( K key ){
        for (int i = 0; i < count; i++) {
            Node<K,V> n = table[i];
            // if( n.key == key || ( n.key != null && n.key.equals(key) ) ) {
            if( Objects.equals( n.getKey(), key ) ) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {
        return count == 0 ;
    }

    @Override
    public boolean containsKey(Object key) {
        return indexOf((K)key) != -1 ;
    }

    @Override
    public boolean containsValue(Object value) {
        return false;
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> m) {

    }

    @Override
    public void clear() {

    }

    @Override
    public Set<K> keySet() {
        // Set 是 Collection 的子接口
        Set<K> keys = new HashSet<>();
        for (int i = 0; i < count; i++) {
            Node<K,V> node = table[i];
            keys.add( node.getKey() );
        }
        return keys;
    }

    @Override
    public Collection<V> values() {
        Collection<V> x = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            Node<K,V> node = table[i];
            x.add( node.getValue() );
        }
        return x;
    }

    @Override
    public Set< Map.Entry<K, V> > entrySet() {
        Set< Map.Entry<K, V> > entries = new HashSet<>();
        for (int i = 0; i < count; i++) {
            entries.add( table[ i ] );
        }
        return entries;
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append('{');
        for (int i = 0; i < count; i++) {
            Node<K,V> node = table[ i ];
            b.append( node.getKey() );
            b.append( '=' );
            b.append( node.getValue() );
            b.append( ',' );
        }
        int index = b.lastIndexOf( "," );
        if( index != -1 ) {
            b.deleteCharAt( index );
        }
        b.append('}');
        return b.toString();
    }
}
